package com.business.system.api;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.DateFormat;
import net.sf.rose.util.StringUtil;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.business.system.action.BaseAction;
import com.business.system.bean.GoodsOrdersBean;
import com.business.system.bean.OrderGoodsBean;
import com.business.system.bean.UserAddressBean;
import com.business.system.util.DBTools;

/**
 * @author: xudy
 * @date: 2018/04/12 8:57
 * @description:商城API接口
 */
@RestController
@RequestMapping("api/shopmall")
public class ApiShopMallAction extends BaseAction {

    /**
     * 商城首页
     * @param service
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping("/goodsList.do")
    public List<Map<String,Object>> goodsList(Service service, HttpServletRequest request,String userId){
        //1.首先获取全部的品牌
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>> myBrand = null;
        try {
            myBrand = brandService.getMyAllBrand(service,userId);
            if(myBrand!=null && myBrand.size()>0){
                for(Map<String,Object> brand : myBrand){
                    Map<String,Object> data = new HashMap<>();
                    data.put("brandName",brand.get("BRAND_NAME"));
                    //根据品牌id获取对应的商品
                    String brandId = brand.get("BRAND_ID").toString();
                    List<Map<String,Object>> goodsList = goodsService.brandGoodsByBrandId(service,brandId);
                    data.put("goodsList",goodsList);
                    result.add(data);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取全部地址
     * @param service
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping("/getUserAddress.do")
    public List<UserAddressBean> getUserAddress(Service service, HttpServletRequest request,String userId){
        List<UserAddressBean> addressList = DBTools.getBeanList(service,UserAddressBean.class,"select * from user_address where user_id=?",userId);
        return addressList;
    }

    /**
     * 新增和编辑地址(包括了)
     * @param service
     * @param address
     * @return
     */
    @RequestMapping("/saveAddress.do")
    public boolean saveAddress(Service service,@ModelAttribute UserAddressBean address){
        boolean result = false;
        if(address!=null){
            if(address.getIsDefault()==1){
                DBTools.updateSql(service,"update user_address set is_default=2 where is_default=1 and user_id=?",address.getUserId());
            }
            result = DBUtils.update(service,address)>0;
        }
        return result;
    }

    /**
     * 删除地址
     * @param service
     * @param id
     * @return
     */
    @RequestMapping("/deleteAddress.do")
    public boolean deleteAddress(Service service,String id){
        return DBTools.deleteBean(service,UserAddressBean.class,id)>0;
    }

    /**
     * 创建订单(返回订单信息)
     * @param service
     * @param orders
     * @param goodsList
     * @param userAddress
     * @return
     */
    @RequestMapping("createOrder.do")
    public GoodsOrdersBean createOrder(Service service,String orders,String goodsList,String userAddress){
        GoodsOrdersBean orderBean = StringUtil.parse(orders,GoodsOrdersBean.class);
        //将该用户的全部地址设置成非默认
        DBTools.updateSql(service,"update user_address set is_default=2 where is_default=1 and user_id=?",orderBean.getUserId());
        //将当前地址设置成默认
        UserAddressBean address = StringUtil.parse(userAddress,UserAddressBean.class);
        address.setIsDefault(1);
        DBUtils.update(service,address);
        //1.创建订单
        orderBean.setAddress(address.getUnionAddress()+" "+address.getAddress());
        orderBean.setUserName(address.getUserName());
        orderBean.setMobile(address.getMobile());
        orderBean.setStatus(GoodsOrdersBean.TO_PAY);
        orderBean.setOrderTime(new Date());
        orderBean.setOrderNo(DateFormat.format(new Date(),DateFormat.DATE_FORMAT_6));
        int i = DBUtils.update(service,orderBean);
        //2.创建订单商品
        if(i>0){
            List<OrderGoodsBean> goodsBeans = StringUtil.parseList(goodsList,OrderGoodsBean.class);
            for(OrderGoodsBean goods : goodsBeans){
                goods.setOrdersId(orderBean.getId());
            }
            int[] j = DBTools.bacthInsertBean(service,OrderGoodsBean.class,goodsBeans);
            if(j.length==goodsBeans.size()){
                return orderBean;
            }
        }
        return null;
    }

    /**
     * 取消订单
     * @param service
     * @param orderId
     * @return
     */
    @RequestMapping("cancleOrder.do")
    public boolean cancleOrder(Service service,String orderId){
        GoodsOrdersBean orders  = new GoodsOrdersBean();
        orders.setId(orderId);
        orders.setStatus(GoodsOrdersBean.IS_CANCER);
        return DBUtils.update(service,orders)>0;
    }



}